package com.wyl.rpc.client;

import com.wyl.rpc.codec.RpcDecoder;
import com.wyl.rpc.codec.RpcEncoder;
import com.wyl.rpc.codec.RpcRequest;
import com.wyl.rpc.codec.RpcResponse;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

/**
 * @author: wyl
 * @date: 2021/8/17
 * @des:
 */
public class RpcClientInitializer extends ChannelInitializer<SocketChannel> {


    /**
     * 初始化通道
     *
     * @param ch
     * @throws Exception
     */
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline cp = ch.pipeline();
        // 添加编解码器
        cp.addLast(new RpcEncoder(RpcRequest.class));
        // @param lengthFieldOffset -> the offset of the length field
        cp.addLast(new LengthFieldBasedFrameDecoder(65536, 0, 4, 0, 0));
        cp.addLast(new RpcDecoder(RpcResponse.class));

        //	添加实际的业务处理器RpcClientHandler
        cp.addLast(new RpcClientHandler());
    }
}
